Wordpress: Echo PHP depending on page ID - php

Good afternoon, I'm trying to display posts using PHP in the template, but I'm using the same template to output different posts depending on the age ID.
I currently have this code which works...
<?php //GET MEMBERS ?>
<?php query_posts('category_name=members&orderby=date'); ?>
<div class="row-fluid">
<ul class="thumbnails">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="span4">
<div class="thumbnail">
<?php // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
?>
<div class="pad">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
But I need to output a different "category" depending on page ID... E.G
if page id is 7 echo "php script"
else page id is 13 echo "different php script"
Thanks, Brad

Wordpress has some built in functions. You can use get_the_ID() to return the ID number to use in your if statements.
Regards

Related

featured post same as post content

I have a wordpress site I am creating.
on the post pages, I have text widget with PHP allowed, where I have a custom loop:
<?php $my_query2 = new WP_Query("showposts=1&cat=9,10,11,18,19&orderby=rand"); ?>
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>">
<div class="home-widget-thumb">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('home-thumb');
}
?>
</div>
<h2><?php the_title(); ?></h2></a>
<div class="body">
<?php echo get_the_excerpt(); ?>
</div><!--body-->
</br>
<span class="more-link">
[more]
</span>
<?php endwhile; ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
For some reason, whatever blog post you are on is the same as in the loop I created.
See an example here:
http://counselingandtraining.com/play-therapy/
The loop for the post is not modified.
Can anyone tell me why this is happening?
Let me know if I can provide further info.
Thanks in advance for your time.
Chris
It looks to me like the problem is that you are still using the base query.
You can change this by adding your variable $my_query2 in the code like this:
<?php if ($my_query2->have_posts()) : ?><?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
This will make all the functions like the_title(), the_content(), etc work as intended since they will be set to $my_query2.

PHP codes for wordpress

Hello guys so I am making a car review wordpress magazine and I am having issues with php codes as I am not a great programmer. Actually on this page Memes I would like the social plugins to be below each picture and not on the top furthermore I would like it to display the post date and the author of the post. Some formatting for the pictures size etc would be great too. Below is the code I am using
<?php /*
Template Name: ListPostsInCategoryThatHasSameNameAsPage
*/ ?>
<?php get_header(); ?>
<div id="content" class="archive <?php if(get_option('colabs_layout_settings')=='two-col-right'){echo 'right';}else{?>left<?php }?>">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class(); ?> id="post_<?php the_ID(); ?>">
<span id="map"><?php _e('Home','colabsthemes');?> » <?php the_title(); ?></span>
<h2 class="title"><?php the_title(); ?></h2>
<div class="entry" style="padding-top:15px;">
<?php the_content(__('<p>Read the rest of this page »</p>','colabsthemes')); ?>
<?php echo colabs_share();?>
<?php wp_link_pages(array('before' => __('<p><strong>Pages:</strong>','colabsthemes'), 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<div id="sidebar" class="<?php if(get_option('colabs_layout_settings')=='two-col-right'){echo 'left';}else{?>right<?php }?>">
<?php get_sidebar(); ?>
</div>
<div> <?php get_footer(); ?> </div>
Finally for the review part I would like to do something like Autotest/review but I don't know which code to use to show these kind of square articles. Please help me out.
For adding the author and date of the post, you can add:
<p>Written by:
<?php get_the_author(); ?></p>
get_the_author() will display the author's name as is set in their "Display name publicly as" field in their user profile (Administration > Users > Your Profile).
To also display the date:
<p>Written by:
<?php get_the_author(); ?> on
<?php get_the_date(); ?></p>
I don't see where you're using the social sharing links on the page you linked to - did you remove them?
For more information:
http://codex.wordpress.org/Function_Reference/get_the_author
http://codex.wordpress.org/Function_Reference/get_the_date

how to get page link of searched result in wordpress?

i tried this code in the read more button i want the link of page
<div class="blogleft">
<ul class="eachblog">
<?php while ( have_posts() ) : the_post(); ?>
<li>
<h1>
<?php the_title();?>
</h1>
<div class="perpostbg">
<?php $content = get_the_content(); echo substr($content,0,400); echo '...';?>
Read more
</div>
</li>
<?php endwhile;?>
</ul>
</div>
use permalink
Read more
OR
Read more
Just write
Read more
This code in your search loop

Getting the post title in index php in a while loop

I have
<?php while ( have_posts() ) : the_post(); ?>
<div class="boxes-third boxes-first">
<div class="latestthree">
<div class="title">
<?php get_the_title($id); ?> // I am trying to get the post title here but doesn't work
<span class="titlearrow"></span>
</div>
<div class="latestthreeimage">
<a rel="prettyPhoto" title="<?php get_the_title($id); ?>"> /same here
<?php the_post_thumbnail(array(300,133)); ?>
</a></div>
<div class="text">
Here i would like to get the excerpt of the post that is no longer than 25 words
<span class="textarrow"></span>
</div>
</div>
</div>
<?php endwhile; ?>
I am trying to do the above mentioned, but did not worked and on the last one did not find related info. I am using wp 3.7.1. Please help me.
You have used get_the_title() which does not print. To print out the title, add an extra echo:
<?php echo get_the_title(); ?>
Or you can use the_title(), which also prints:
<?php the_title();?>
Try just using,
the_title();
Reference.

How to combine is_category() and query_posts() in an if..else statement?

I have a few snippets of code where I want to check if the category is correct and if post actually exist, at the moment I have an if_category() function that then queries my required category but I think these are clashing so none of my posts for the category are being displayed, can anyone advise how I can fix this code?
PHP
<?php if(is_category('Rainwater Harvesting Product')) { ?>
<!-- Related Projects -->
<?php query_posts('category_name=rainwater-harvesting-project&showposts=5'); ?>
<?php if (have_posts()) { ?>
<div class="aside-mod related-projects">
<div class="curved-ctn">
<h2 class="module-header">Related Projects</h2>
<ul class="project-list clearfix">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="project">
<a href="<?php the_permalink();?>" class="project">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
} ?>
<h3><?php the_title(); ?></h3>
<!-- <i class="icon-project"></i> -->
</a>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>
</div>
<?php } ?>
<?php } ?>
<?php wp_reset_query(); ?>
It does not work because in your
<?php query_posts('category_name=Greywater Recycling Project&showposts=5'); ?>
you are giving to category_name, the Name instead of the category slug, the query_posts parameter, category_name takes only the slug. eg: it must be
<?php query_posts('category_name=greywater-recycling-project&showposts=5'); ?>
More info here: http://codex.wordpress.org/Function_Reference/query_posts

Categories