• Hi there..

    I hope someone can help:

    I’m trying to list links to every posts along my header, where links to the pages usually are.

    I want to create a link to every post in any category, using a custom field for each post as the link text (instead of just the title).

    At the moment, here is the code that lists pages along the bottom of the header:

    CODE:

    <ul id="nav" class="clearfloat">
    <li><a href="<?php echo get_option('home'); ?>/" class="on">Home</a></li>
    <?php wp_list_pages('title_li='); ?>
    </ul>

    How can I edit this to show posts instead, and with custom fields for the link text.

    Thank you to anyone kind enough to help out ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • <?php
    //get four posts in category 8, then for each post, get custom fields,
    //link_url and link_text, and display that info as a link
    $args=array(
      'showposts' => 4,
      'category__in' => array(8),
      'caller_get_posts'=>1
    );
    $posts=get_posts($args);
    if ($posts) {
      foreach($posts as $post) {
        setup_postdata($post);
        $link = get_post_meta($post->ID, 'link_url', true);
        $text = get_post_meta($post->ID, 'link_text', true);
        if ($link){
          echo  '<a href="'.$link .'">'.$text.'</a>';
        }
      }
    }
    ?>
    Thread Starter wukka-wukka

    (@wukka-wukka)

    thanks so much for your quick reply.

    please excuse my WP dumbness (i’m new to this)…
    but if I already have some custom fields for each post, what should i name the new one (that i want for the link text) .

    thanks

    Thread Starter wukka-wukka

    (@wukka-wukka)

    also… i tried replacing that code with the one i originally offered, and the whole page stopped working.

    i’m sure i’m missing something very obvious here, but i just dont know php well enough to know what the hell is going on.

    is there any way you could break it down really simple for me?

    if it helps… this is the full header page i’m trying to get working:

    <?php load_theme_textdomain('branfordmagazine'); ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="https://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
    
    <head profile="https://gmpg.org/xfn/11">
    <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
    <title>
    <?php bloginfo('name'); ?>
    <?php if ( is_single() ) { ?>
    &raquo;
    <?php
    foreach((get_the_category()) as $cat) {
    echo $cat->cat_name . ' ';
    } ?>
    <?php } ?>
    <?php wp_title(); ?>
    </title>
    
    <meta name="generator" content="WordPress <?php bloginfo('version'); ?>" />
    <!-- leave this for stats -->
    <?php wp_head(); ?>
    
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>"type="text/css" media="screen" />
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/styles/nav.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/styles/plugins.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/styles/template-style.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/styles/print.css" type="text/css" media="print" />
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/styles/ui.tabs.css" type="text/css" media="screen" />
    
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery-1.2.2.pack.js"></script>
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/dropdowns.js"></script>
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/ui.tabs.pack.js"></script>
    
       <script type="text/javascript">
                $(function() {
                    $('#container-4 ul').tabs({ fxFade: true, fxSpeed: 'fast' });
    
                    $('#container-11 ul').tabs({ event: 'mouseover' }).find('a').click(function() {
                        return false;
                    });
                });
            </script>
    
    <link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
    <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
    
    </head>
    
    <body<?php if ( is_home() ) { ?> id="home"<?php } ?>>
    <div id="page" class="clearfloat">
    
    <div class="clearfloat">
    <div id="branding" class="left" onclick="location.href='<?php echo get_settings('home'); ?>';" style="cursor: pointer;">
        <div class="blogtitle" ><a href="<?php echo get_option('home'); ?>/"><?php //bloginfo('name'); ?></a></div>
    	  <div class="description"><?php //bloginfo('description'); ?></div>
    </div>
    
    <div class="right"><?php include (TEMPLATEPATH . '/searchform.php'); ?></div>
    </div>
    
    <ul id="nav" class="clearfloat">
    <li><a href="<?php echo get_option('home'); ?>/" class="on">Home</a></li>
    <?php wp_list_pages('title_li='); ?>
    </ul>

    Okay, how about if you just intall the Pages-Links-To plugin, then create a page for each post you want listed in your navigation, and in each Page’s Page Links To field, put the URL of the post you want to link to.

    https://txfx.net/code/wordpress/page-links-to/

    Thread Starter wukka-wukka

    (@wukka-wukka)

    Hi Michael ??

    Unfortunately, I dont think I shuld use the pages-links-to plugin because the blog is designed for getting search engine traffic, and I will lose a lot of internal linking juice if I link to redirect pages from the header. I need to link to each individual post from the header. That way, every page and post on my blog has links to all my posts (I only have 6 in total), improving my page rank for each post.

    I had no idea what I was asking was anything strange or tricky. I thought there would be some kind of list_posts function, like you get for the browse categories widgets. Then just a bit of code that pulls the custom field data for the link text.

    Is there no easy way to do this?

    If not, please just give me the hard version, and try to be gentle with the explanation ??

    Thanks for your help . I appreciate it ??

    My example assumed your custom fields were called “link_url” and “link_text”, so replace with your custom field names. The code I suggested would replace your wp_list_pages line.

    If you can’t get it working, and since you only have 6 posts, you could always hard-code those links in place of the wp_list_pages.

    Thread Starter wukka-wukka

    (@wukka-wukka)

    Thanks for the advice, Michael, I will try those suggestions ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘List Posts With Custom Fields as Link Text?’ is closed to new replies.