Pagination Wordpress in Custom Theme - php

Hi I hope someone can help with this.
I'm building a web site using wordpress, but I created my custom themes.
I just have basic knowledge of php and i managed to build the home page which show the latest 4 post.
This is the code that i use to show the last and the second-last post. Just using an offset and skipping one post every time to show the previous.
LATEST POST SCRIPT
<?php $posts = get_posts('cat=1,4,5&numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>
<?php echo catch_that_image() ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
SECOND LAST POST SCRIPT
</span></td>
<td colspan="8" rowspan="2" valign="top" class="blog_text">
<?php
query_posts('showposts=1'); ?>
<?php $posts = get_posts('cat=1,4,5&numberposts=1&offset=1'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>
<?php the_content(); ?>
<?php $count2++; } ?>
<?php endforeach; ?>
I hope this part help to understand, because i'm working in local so i can't show the website for now.
Now my problem is how i can dynamic implement the pagination? so if the first page show the post 1,2,3,4
i need to create a dynamic pagination which would allow me on clicking "previous posts" or the number of the page
will show the post number 4,5,6,7.
I've already had a look on the tutorial for dynamic pagination but i can't understand how i can do it, as i'm working on wordpress
and my script are different! As i said i have basic knowledge of PHP so if someone could explain me how to do with my code would be perfect!
Probably i don't need the foreach because this was a code found to show from post 1 to post 4 for example, and i changed to show just one!
I basically can't use a normal loop because the post that i have to show are in diferrent postion so i need separate code!
I hope i could explained myself! Thank you to everyone for any help.

Related

How to only show category description and title if there's a description available?

Is it possible to only show the code below if there's a category description?
<h2>About <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h2>
<?php $catID = get_the_category(); echo category_description ( $catID[0] ); ?>
I have these codes on my single posts in Wordpress but sometimes, I forgot to add descriptions on a new category that I added. So when a user visits the post, they will just see the word About Category and no description at all, making it looks like an incomplete article.
I'm not a developer and I'm also not familiar with PHP. I only added that code on the single.php to show the description. But I want it not to show when there's no description available.
Hope someone can give me the exact code to make it work.
Thanks!
Good practice to assign your values into variables at the top of your script before entering into HTML whenever possible. This will help prevent you making redundant calls and to debug your code better. Once you have your assigned values, you'll want to check if the value is empty. I am assuming the code you presented will always return some kind of value for index 0.
<?php
$cat = get_the_category();
$cat_0 = $cat[0];
$cat_0_name = $cat_0->cat_name;
$cat_0_desc = category_description($cat_0);
?>
<?php if(!empty($cat_0_desc)): ?>
<h2>About <?php echo $cat_0_name; ?></h2>
<?php echo $cat_0_desc; ?>
<?php endif; ?>
I should like to point out that I am choosing to use an alternative syntax for control structures versus the traditional brace control. This will make your code more readable and easily debugged when mixed in with HTML.
If your code is still throwing you errors, I would suggest you check your error logs as something would be happining during the get_the_cateory() call or that it's not returning any values resulting in error with $cat[0].
<?php if ($cat = get_the_category() && count($cat)>0) { ?>
<!-- <?php echo print_r($cat,1); ?> -->
<h2>About <?php echo $cat[0]->cat_name;?></h2>
<?php echo category_description ($cat[0]->cat_id); ?>
<?php } ?>

Integrating Wordpress page into existing HTML site

I've run into a bit of a wall with a site I'm designing for a client. This was originally a very low budget static site but the client now needs the ability to edit some content from time to time. I've heard that you can just embed a wordpress page into existing HTML site and have followed the steps on the Codex site but can't seem to get it working. Any help is greatly appreciated.
So the wordpress page I'm trying to embed is the following:
http://octagonclubmiami.com/cs/
And the code I'm using is as follows:
This is posted at the beginning of the php file
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('cs/wp-blog-header.php');
?>
And this is within the body of the php file
<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php
endforeach;
?>
The actual page I'm trying to embed into is the following:
http://octagonclubmiami.com/community_service_test.php
As it sits it's currently displaying I guess some sample post instead of the page mentioned earlier.
Thanks in advance!
The better solution (I think) is to use get_post() and specify the ID number of the post/page you wish to output:
<?php
$page = get_post( 'ID NUMBER HERE' );
setup_postdata( $page );
?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
You can identify the post's ID number in the WordPress back-end.

Zend_Feed_Rss Manipulation for Magento newsfeed

I have created a newsfeed in my magento site using Zend Framework. This works almost perfectly except, I want to retrieve only the first 3 entries on the rss. If I try to do this, the first 3 items are displayed on my site but the foreach loop continues to execute so excess spaces and html elements are added in my site. How can I retrieve only the first 3 entries of the rss? Here's how my code looks:
<?php $i = 0;
<?php $channel = new Zend_Feed_Rss('http://mydomain/newsfeed'); ?>
<?php foreach ($channel as $item):
<div>
if($i<3): ?>
<label>My feed title is: <?php echo $item->title; ?>
<?php endif; $i = $i + 1; ?>
</div>
<?php endforeach; ?>
I have about 10 entries on the newsfeed so if I execute something like this, I get the first 3 properly, then I get 7 excess labels with My feed title is:. I tried, using break but this broke my entire page so I can't use that. Can someone please guide me to the right direction?
you can try with for loop instead of foreach
for($i=0; $i<=min(3, count($channel->title)); $i++) {
$feed_title = $channel->title[$i];
// do something
}
hope this will sure solve your issue.

Looping through an array in Wordpress

I have created a custom post type with an image gallery upload. Now I am trying to display the gallery on the front end. This is what I have so far that works to display 1 image, but if multiple images are uploaded all the urls get stuck in the src tag. So I'm guessing I should loop through that array and spit out each one separately? Would that be the route to go and if so how can I accomplish this? Any help is appreciated.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
echo '<img src="'.get_post_meta($post->ID, 'gallery-upload', true).'">';
?>
<?php endwhile; else: ?>
<p><?php _e('No posts were found. Sorry!'); ?></p>
<?php endif; ?>
EDIT:
This is what is being returned:
<img src="http%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F800x400-volbeat-mock1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1574_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1576_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1576_4_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F2244_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F300789_2349086884438_1168050047_32154880_1451576942_n.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F373795_278881222158106_278880528824842_834930_1454244548_n.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F20110909-121141.jpeg">
Looking at the function reference, get_post_meta ordinarily returns an array unless the third argument is set to true. Something like this should work, more or less.
<?php
foreach(get_post_meta($post->ID, 'gallery-upload') as $meta) {
foreach(explode(',', $meta) as $src) {
echo '<img src="'.htmlentities($src).'">';
}
}
?>
EDIT: Apparently gallery-upload is stored as comma-separated values. Updated my snippet above to hopefully account for this.

How do you stop echo of multiple values with 'foreach' in PHP?

How do you when using custom fields in Wordpress echo just the first value using foreach?
Currently the code is:
<?php for(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php endforeach; ?>
This takes the field from the wordpress page (the field is a link to another page), creates a link to that page using get_permalink but when I want to echo the page title it does it, but then it also echos all other values that are not needed.
If you just want the execute the first iteration of the loop, try this:
<?php foreach(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php break; ?>
<?php endforeach; ?>
Wouldn't it then be easier just to use the first element of the returned array? Maybe Wordpress offers other filters that return the the page's title only.
you can just add
$counter = 0;
<?php for(get_field('venue_event') as $post_object): ?>
$counter++;
if($counter == 1)
{
<?php echo get_the_title($post_object) ?>
}
<?php endforeach; ?>

Categories