homepage Welcome to WebmasterWorld Guest from 184.73.52.98
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
include page id only
working in wordpress theme
trucqulent




msg:4456560
 7:13 pm on May 22, 2012 (gmt 0)

hey guys!

so im working on wp theme where the owner wanted a list added to a specific spot on the page.

that went well.

Now, he wants to display the list ONLY on specific pages (7 out of 20 or so total).

here is the actual list:



<div id="rightbottommenucon"><link rel="stylesheet" href="page.css" type="text/css"/>

<ul id="rightbottommenu">

<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
</ul>
</div>


The above code is placed in the "rightside.php" file.

so in the end, Im looking for a way to simply add a line of php to that list basically saying " include list if page id= ___,___,___"


thanks for the help.

 

cffrost2




msg:4456700
 11:45 pm on May 22, 2012 (gmt 0)

I would store your page id's in an array, and check if it exists. If so, then include the list

<?php
if(isset($_GET['pageid']) ? $_GET['pageid'] : '')
{
$id = $_GET['pageid'];
$pages = array('id1', 'id2', 'id3');
foreach($pages as $page)
{
if($page == $id)
$match = true;
}
if($match)
include('list.php');
}
?>


This is assuming you have the list in a different file.
If not, then...

<?php
if(isset($_GET['pageid']) ? $_GET['pageid'] : '')
{
$id = $_GET['pageid'];
$pages = array('id1', 'id2', 'id3');
foreach($pages as $page)
{
if($page == $id)
$match = true;
}
if($match)
{ ?>
<div id="rightbottommenucon"><link rel="stylesheet" href="page.css" type="text/css"/>

<ul id="rightbottommenu">

<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
</ul>
</div>
<?php }

}
?>


There may be a better way but that's what comes to mind.

Hope that helps

trucqulent




msg:4456705
 12:00 am on May 23, 2012 (gmt 0)

mistake was here.

[edited by: trucqulent at 12:29 am (utc) on May 23, 2012]

trucqulent




msg:4456716
 12:14 am on May 23, 2012 (gmt 0)

disregard the previous statement. I tried the second option and im getting an error.
Parse error: syntax error, unexpected $end in /home/content/a/v/#*$!xx/html/wp-content/themes/#*$!#*$!xx/rightside.php on line 219

I added the ids in the array line, am I meant to edit any other area in that code? sorry, just very new to php... mainly do front end stuff :)

trucqulent




msg:4456791
 2:58 am on May 23, 2012 (gmt 0)

I ended up using this:

<?php
$post_id = get_the_ID();
$ids = array( 241, 265,254,260,258,263,268,189);
if( in_array( $post_id, $ids )) : ?>
<?php endif; ?>

error free, but the code dosnt stop the div from showing up on the pages not in the array.

anyone have any suggestions?

cffrost2




msg:4456960
 2:30 pm on May 23, 2012 (gmt 0)

I double checked my second clip of code and it worked on my server. But yours will work.

I just used this code and it works on my end.

<?php
if(isset($_GET['pageid']) ? $_GET['pageid'] : '')//I'm using this to check for the var before executing the code. You may a different way of getting the var such as a function.
{//begin if isset
$post_id = $_GET['pageid'];
$ids = array( 241, 265,254,260,258,263,268,189);
if( in_array( $post_id, $ids )) : ?>
<div id="rightbottommenucon"><link rel="stylesheet" href="page.css" type="text/css"/>

<ul id="rightbottommenu">

<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
</ul>
</div>
<?php endif;
}//end if isset ?>


I tested on a test.php page and visited test.php?pageid=241 and it showed the list. I changed the pageid var to 240 and it didn't show the list.
Not sure why it's not working for you.

trucqulent




msg:4456969
 2:52 pm on May 23, 2012 (gmt 0)

Im getting syntax errors. I cant seem to find anything in the code that's wrong though.

cffrost2




msg:4457012
 4:03 pm on May 23, 2012 (gmt 0)

It may be something conflicting with code used by w-p. I'm not sure what or why. But testing the above as a single page, it works perfectly. There isn't enough here to further diagnose. I wish I could assist more.

rocknbil




msg:4457017
 4:40 pm on May 23, 2012 (gmt 0)

This works? Really? I don't see how it can.

if(isset($_GET['pageid']) ? $_GET['pageid'] : '') {

endif;
}

It definately will work as

if(isset($_GET['pageid'])) {
$page_id = (is_numeric($_GET['pageid']) and ($_GET['pageid'] > 0)?$_GET['pageid']:null;
// etc
}

But I don't know that you're going in the right direction. The original task:

Now, he wants to display the list ONLY on specific pages (7 out of 20 or so total).


This part is "sorta" correct:

<?php
$ids = array( 241, 265,254,260,258,263,268,189);
if( in_array( $post_id, $ids )) { ?>
The list outputs here
<?php } ?>

But even that relies on hard coding in your programming, and if you need to change it by adding to/removing from the id list, you need to modify the programming. It takes the whole concept of a user-managed CMS out of the hands of the user. This can get very annoying, very fast, on both your part and the part of the client. Both are "bad." :-) (If you're going to do that, you may as well just code static pages.) Secondly under various conditions you'd never predict, it's possible the ID's might change.

Here's a much more forward thinking approach.

1. For the pages you want to include this on, create a custom field. Let's say you name it "include_link_list". You can put anything you want in the value field: Yes, 1, blah, doesn't matter, just have it hold a value. An aside, if there are different kinds of lists, you can use this value for conditionals in your programming to output various lists (e.g., 'vendors','customers','testimonials', whatever.)

2. In page.php, now just check to see if it's got the meta key (or check the value, if multiple lists.)

$list_page = get_post_meta($page_id,'include_link_list',1);

if (! empty($list_page)) { echo some_list($list_page); }

3. Now write a function in functions.php to return the list. I've named it "some_list" here, and included a multi-list option, but it should default to a single list if you don't pass a value. That is,

echo some_list();

should fall in the "else"


function some_list($listvalue=null) {
$list = null;
if ($listvalue=='vendors') {
$list = '
<ul id="vendorlist">
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
</ul>
';
}
else if ($listvalue=='customers') {
$list = '
<ul id="customerlist">
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
</ul>
';
}
else {
$list = '
<ul id="linklist">
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
<li><a href="link">title</a></li>
</ul>
';
}
return $list;
}


Now, if you **really** want to play it smart, you'll have a document somewhere that contains these actual lists and correspond the slug to the list value in the custom field. The document doesn't need to display on the site anywhere, but once again you are putting the content modification in the hands of the customer. So say you have documents with a slug that corresponds the the above list types. The slug is post_name in WP. The content of these documents are a simple list, like above. So ...


function some_list($listvalue=null) {
$list = null;
if (! $listvalue) { $listvalue=='linklist'; }
// I detest globals, but it's Wordpress. :-\
global $wpdb,$table_prefix;
$table = $table_prefix . 'posts';
$query = "select post_content from $table where post_name='$listvalue' and post_type='page'";
// This is an array, but there should only be one . . .
$posts = $wpdb->get_results($query);
// hence $posts[0], the first array element.
$list = $posts[0]->post_content;
return $list;
}


Again, typed on the fly, can probably use a little tweaking, but hard coding values in your programming is not productive in the long run.

trucqulent




msg:4457071
 7:22 pm on May 23, 2012 (gmt 0)

Hey guys,

I actually got it working.
@cff - thank you for your help.
@rock - thank you for taking the time to go into so much detail.

rocknbil




msg:4457460
 4:38 pm on May 24, 2012 (gmt 0)

I usually don't care but I'm curious - did you put the content back into Wordpress instead of your programming? :-)

trucqulent




msg:4457471
 5:08 pm on May 24, 2012 (gmt 0)

Originally I did. At the time I was in panic mode and just needed the thing to function. Last night I quietly went back and created a separate php file. Im trying to learn 5 languages, database, 4 development platforms, 3 CMS's, and an assortment of bells & whistles... all while taking the small job here and there that tests my skill set. In the event I need something to be done "now" i kinda have to go with the path of least resistance so the site owner is happy. However, Ill usually go back and play with things/methods after the fact (usually in the wee hours of the morning when no one is looking!)

trucqulent




msg:4461066
 4:35 am on Jun 4, 2012 (gmt 0)

Hey Rock,

I have a few questions for you regarding the use of adding additional kinds of lists, or in this case, menus. ( I would have opened another thread, but considering its still on topic Ill keep this one going).

so Im in a situation now whereas, Ive added an additional menu location to wordpress.

Now that the location is there, available, and registering on the wp menu feature in the admin panel, I'd like the ability to include separate lists depending on the page the user is viewing.

For example:

Lets say we are looking at the main nav menu. the nav menu holds 5 tabs. below that nav, there will be a sub-nav bar which we want to change depending on which page the user is currently viewing. (essentially each page on the nav acts are a category, while the sub-nav should act as the sub-category)

so for example:

Assuming (the viewer is viewing page2 of the nav bar)

PAGE1 PAGE2 PAGE3 PAGE4 PAGE5
V
PAGE2SUB1 PAGE2SUB2 PAGE2SUB2 PAGE2SUB4 PAGE2SUB5

What would be the best way to handle this, assuming the end goal is to keep control of the content management in the hands of the user?

I was going with what you originally suggested, but then I started to wonder... would I need to add another location for each kind of list? since the list is the "array", and for all intents, the "array" is the menu that owner would make in the wp admin function?

Here is what I had so far.


wall o text inc (sorry for the mass post)



<?php>


/* in page.php*/

<div id="content" role="main"> /* the actual page div*/

<div id="access" role="navigation"> /* the sub nav menu div*/
<?php /* Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff */ ?> /*theme notes*/
<div class="skip-link screen-reader-text"><a href="#content" title="<?php esc_attr_e( 'Skip to content', 'mantra' ); ?>"><?php _e( 'Skip to content', 'mantra' ); ?></a></div>
<?php /* Our navigation menu. If one isn't filled out, wp_nav_menu falls back to wp_page_menu. The menu assiged to the primary position is the one used. If none is assigned, the menu with the lowest ID is used. */ /* theme notes*/
wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'secondary' ) ); ?>

$list_page = get_post_meta($page_id,'include_link_list',1);
if (! empty($list_page)) { echo some_list($list_page); }
</div>


/*in functions.php*/
function secondary_menu($listvalue=null) {
$list = null;
if ($listvalue=='blog') {
$list = '
<ul id="vendorlist"></ul>';
}
else if ($listvalue=='streaming') {
$list = '<ul id="customerlist"> </ul>';
}
else if {$list = '<ul id="recorded"></ul>';}
return $list;
}

/* in a seperate file*/
function secondary_menu($listvalue=null) {
$list = null;
if ( ! $listvalue) { $listvalue=='linklist';}}
global $wpdb,$table_prefix;
$table = $table_prefix . 'posts';
$query = "select post_content from $table where post_name='$listvalue' and post_type='page'";
$posts = $wpdb->get_results($query);
$list = $posts[0]->post_content;
return $list;
}

$Blog = array(blog_menu_create_in_wp_menu ); /* <---- is that right? */
$recorded = array(recorded_menu_create_in_wp_menu);
$streaming = array (streaming_menu_create_in_wp_menu);


?>

rocknbil




msg:4461286
 4:47 pm on Jun 4, 2012 (gmt 0)

Mmm, I don't get this at all. I don't see the function you've written for "some_function" and you now have two functions "secondary_menu" that act differently. which one the script uses is a coin toss (whatever one it gets to first, I think.)

To attempt to answer the question, it sounds to me like you should explore the wordpress child_of and is_page() to construct your menus. That is, the menus are children of the parent, and **this** page can be determined by is_page(), and the "if" logic can automate the menus for you.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved